package com.longxiao.validator.parser;


import com.longxiao.validator.ValidResult;
import com.longxiao.validator.anno.ValiCnMobile;

import java.lang.annotation.Annotation;

/**
 * 验证中国手机号
 * Created by houhongwei on 2017/5/12.
 */
public class ValiCnMobileParser extends Parser {

    @Override
    protected ValidResult doValid(Object value, Annotation annotation) {
        ValiCnMobile anno = (ValiCnMobile) annotation;
        String s = null;
        if (value.getClass().isArray()) {
            s = (((String[]) value)[0]);
        } else {
            s = (String) value;
        }
        if (s.length() != 11) {
            return ValidResult.createFail(anno.err());
        }

        if (!"1".equals(s.substring(0,1))) {
            return ValidResult.createFail(anno.err());
        }
        return ValidResult.createSucc();
    }

    @Override
    public Class getAnno() {
        return ValiCnMobile.class;
    }

    @Override
    protected boolean isRequired(Annotation annotation) {
        ValiCnMobile tmp = (ValiCnMobile) annotation;
        return tmp.required();
    }

    @Override
    protected String getErr(Annotation annotation) {
        return ((ValiCnMobile) annotation).err();
    }
}
